package com.neusoft.controller;

import com.neusoft.entity.Rent;
import com.neusoft.entity.Room;
import com.neusoft.entity.common.CommonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("roomConsumer")
public class RoomConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    //    private String url_prefix = "http://localhost:8080";//"http://localhost:8080"
    private String url_prefix = "http://localhost:8001";

    @PostMapping("save")
    public void insert(Room bean) {

        String url = url_prefix + "/room/save";
        System.out.println(bean);
        restTemplate.put(url, bean);
    }

    @RequestMapping("queryAll")
    @ResponseBody
    public Object queryAll(@Param("page") Integer page, @Param("limit") Integer limit, @Param("bean") Room bean) {

        String url = url_prefix + "/room/queryAll/" + page + "/" + limit;
        System.out.println(url);
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        map.put("bean", bean);
        Object result = restTemplate.postForObject(url, bean, CommonResult.class);
        return result;

    }

    @RequestMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id) {
        String uri = url_prefix + "/room/queryById/" + id;
        return restTemplate.getForObject(uri, Room.class);
    }

    @PostMapping("deleteById")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            System.out.println("ids值为：" + ids[i]);
        }
        StringBuffer sb = new StringBuffer();
        System.out.println(Arrays.toString(ids));
        sb.append(Arrays.toString(ids));
        sb.deleteCharAt(sb.indexOf("["));
        sb.deleteCharAt(sb.lastIndexOf("]"));
        System.out.println(sb.toString());
        String url = url_prefix + "/room/deleteById/" + sb.toString();
        return restTemplate.postForEntity(url, sb.toString(), Boolean.class);
    }

    @PostMapping("deleteById1/{id}")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById1(@PathVariable Integer id) {
        String url = url_prefix + "/room/delete1/" + id;
        return restTemplate.postForEntity(url, id, Boolean.class);
    }

    @RequestMapping("toList")
    public String toList() {
        return "user/room_list";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id, Model model) {

        Room bean = (Room) queryById(id);
        model.addAttribute("bean", bean);

        return "user/room_add";
    }

    @RequestMapping("checkin")
    public String checkin(Integer id, Model model) {
        Room bean = (Room) queryById(id);
        int houseid = bean.getId();
        String address = bean.getAddress();
        int amount = bean.getAmount();
        Rent beanrent = new Rent();
        beanrent.setAddress(address);
        beanrent.setHouseid(houseid);
        beanrent.setAmount(amount);
        model.addAttribute("bean", beanrent);
        return "user/rent_add";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("bean", new Room());
        return "user/room_add";
    }

}
